package at.guigu.controller;

import at.guigu.exception.BusinessException;
import at.guigu.exception.MyException;
import at.guigu.exception.SystemException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import java.io.FileNotFoundException;

@ControllerAdvice
public class ProjectEcepAdvice {
    // 捕获指定的自定义异常BusinessException进行处理
    @ExceptionHandler(BusinessException.class)
    public ModelAndView doBusinessException(BusinessException e){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("errorShow1");
        // 发送对应消息传递给用户，提醒规范操作
        return modelAndView;
    }

    // 捕获指定的自定义异常SystemException进行处理
    @ExceptionHandler(SystemException.class)
    public ModelAndView doSystemException(SystemException e){
        // 记录日志
        // 发送消息给运维
        // 发送邮件以及异常发送给开发人员
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("error");
        return modelAndView;
    }

    // 捕获除自定义异常MyException进行处理
    @ExceptionHandler(MyException.class)
    public ModelAndView doMyException(MyException e){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("errorShow5");
        return modelAndView;
    }

    // 捕获除自定义外的其它异常进行处理
    @ExceptionHandler({FileNotFoundException.class, NullPointerException.class})
    public ModelAndView doException(Exception e){
        // 记录日志
        // 发送消息给运维
        // 发送邮件以及异常发送给开发人员
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("error");
        return modelAndView;
    }
}
